import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SqlOperate
 * @Description TDD
 * @Author 混沌酱
 * @Date 2023-08-08 14:13
 * @Version 1.0
 **/
public class SqlOperate {

    private String URL;
    private String USER;
    private String PASSWORD;

    Connection connection;
    Statement statement;


    /**
     *
     * @param url 数据库连接地址
     * @param user  数据库用户名
     * @param password  数据库密码
     */
    public SqlOperate(String url, String user, String password) {
        this.URL = url;
        this.USER = user;
        this.PASSWORD = password;
        init();
    }

    /***
     * @Author 混沌酱
     * @Description //TODO
     * @Date 16:58 2023-08-08
     * @Param []
     * @return void
     **/
    public void init() {
        try {
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            e.getErrorCode();
        }
    }

    /**
     * 释放连接资源
     */
    public void close() {
        try {
            if (statement!=null){
                statement.close();
                statement=null;
            }
            connection.close();
            connection=null;
        } catch (SQLException e) {
            e.getErrorCode();
        }
    }

    /***
     * @Author 混沌酱
     * @Description 执行sql语句
     * @Date 16:59 2023-08-08
     * @Param [sqlCommand]  sql语句
     * @return java.sql.ResultSet
     **/
    public ResultSet exec(String sqlCommand) {
        ResultSet resultSet=null;
        try {
            if (statement==null){
                statement=connection.createStatement();
            }
            resultSet=statement.executeQuery(sqlCommand);
        } catch (SQLException e) {
            e.getErrorCode();
        }
        return resultSet;
    }

    /***
     * @Author 混沌酱
     * @Description  获取当前数据库所有表的表名
     * @Date 17:00 2023-08-08
     * @Param []
     * @return java.util.List<java.lang.String>
     **/
    public List<String> getTables(){
        List<String> list=new ArrayList<>();
        try {
            ResultSet resultSet=exec(SqlCommand.TABLES);
            while (resultSet.next()){
                list.add(resultSet.getString(1));
            }
            resultSet.close();
            close();
        } catch (SQLException e) {
            e.getErrorCode();
        }
        return list;
    }

    /***
     * @Author 混沌酱
     * @Description  获取表的数据信息
     * @Date 17:00 2023-08-08
     * @Param [tableName] 表名
     * @return java.util.List<TableAttribute>
     **/
    public List<TableAttribute> getAttribute(String tableName) {
        List<TableAttribute> list=new ArrayList<>();
        try {
            if (connection==null){
                init();
            }
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet resultSet = metaData.getColumns(connection.getCatalog(),null, tableName, "%");
            while (resultSet.next()) {
                TableAttribute tableAttribute=new TableAttribute();
                tableAttribute.setName(resultSet.getString("COLUMN_NAME"));
                tableAttribute.setNote(resultSet.getString("REMARKS"));
                tableAttribute.setType(new ConvertSqlType(resultSet.getString("TYPE_NAME")).Convert());
                tableAttribute.setIsPrimaryKeys(resultSet.getString("IS_AUTOINCREMENT"));
                list.add(tableAttribute);
            }
            resultSet.close();
        }catch (SQLException e){
            e.getErrorCode();
        }
        close();
        return list;
    }

    /**
     * @Author 混沌酱
     * @Description 获取表主键的类型
     * @Date 9:57 2023-08-11
     * @Param
     * @return
     **/
    public String primaryKey(String tableName) throws SQLException {
        if (connection==null){
            init();
        }
        String primaryKeyType="";
        String cmd=SqlCommand.PRIMARYKEY;
        cmd=cmd.replace("DATANAME",connection.getCatalog());
        cmd=cmd.replace("TABLENAME",tableName);
        ResultSet resultSet=exec(cmd);
        while (resultSet.next()){
            primaryKeyType=resultSet.getString("DATA_TYPE");
        }
        resultSet.close();
        close();
        return new ConvertSqlType(primaryKeyType).Convert();
    }

}
